package com.shijichaoxing.controller;

import com.shijichaoxing.entity.Candidate;
import com.shijichaoxing.service.CandidateService;
import com.shijichaoxing.utils.PageParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Date;

/**
 * @author zj
 */
@Controller
public class CandidateController {

    @Resource
    private CandidateService candidateService;

    @RequestMapping(value = "/candidate/addCandidate.do", method = RequestMethod.POST)
    @ResponseBody
    public boolean addCandidate(
            @RequestParam String realName,
            @RequestParam String password,
            @RequestParam String sex,
            @RequestParam Date birthday,
            @RequestParam String tel,
            @RequestParam String email,
            @RequestParam String address,
            @RequestParam String resumeUrl,
            @RequestParam Date graduationDate,
            @RequestParam Integer interviewerId
    ) {
        Candidate candidate = new Candidate();
        candidate.setRealName(realName);
        candidate.setPassword(password);
        candidate.setSex(sex);
        candidate.setBirthday(birthday);
        candidate.setTel(tel);
        candidate.setEmail(email);
        candidate.setAddress(address);
        candidate.setResumeUrl(resumeUrl);
        candidate.setGraduationDate(graduationDate);
        candidate.setInterviewerId(interviewerId);
        return candidateService.addCandidate(candidate);
    }

    @RequestMapping(value = "/candidate/updateCandidate.do", method = RequestMethod.POST)
    @ResponseBody
    public boolean updateCandidate(
            @RequestParam Integer id,
            @RequestParam String realName,
            @RequestParam String password,
            @RequestParam String sex,
            @RequestParam Date birthday,
            @RequestParam String tel,
            @RequestParam String email,
            @RequestParam String address,
            @RequestParam String resumeUrl,
            @RequestParam Date graduationDate,
            @RequestParam Date employTime,
            @RequestParam Integer interviewerId,
            @RequestParam String comment
    ) {
        Candidate candidate = new Candidate();
        candidate.setId(id);
        candidate.setRealName(realName);
        candidate.setPassword(password);
        candidate.setSex(sex);
        candidate.setBirthday(birthday);
        candidate.setTel(tel);
        candidate.setEmail(email);
        candidate.setAddress(address);
        candidate.setResumeUrl(resumeUrl);
        candidate.setGraduationDate(graduationDate);
        candidate.setEmployTime(employTime);
        candidate.setInterviewerId(interviewerId);
        candidate.setComment(comment);
        boolean result = candidateService.updateCandidate(candidate);
        return result;
    }

    @RequestMapping(value = "/candidate/findCandidateById.do", method = RequestMethod.GET)
    @ResponseBody
    public Candidate findCandidateById(@RequestParam Integer id) {
        Candidate candidate = candidateService.findCandidateById(id);
        return candidate;
    }

    @RequestMapping(value = "/candidate/findCandidateByPage.do", method = RequestMethod.GET)
    @ResponseBody
    public PageParam<Candidate> findCandidateByPage(
            @RequestParam int currPage,
            @RequestParam int pageSize,
            @RequestParam String indexName,
            @RequestParam boolean isAsc) {
        PageParam<Candidate> pageParam = new PageParam<>(currPage, pageSize);
        candidateService.findCandidateByPage(pageParam, indexName, isAsc);
        return pageParam;
    }

    @RequestMapping(value = "/candidate/deleteById.do", method = RequestMethod.DELETE)
    @ResponseBody
    public boolean deleteById(@RequestParam Integer id) {
        boolean result = candidateService.deleteById(id);
        return result;
    }
}
